﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using NetBay.Core.BusinessEntities.Barcodes;
using Telerik.Windows.Controls;
using Rio.Manager.Application.Model;
using System.Linq;

namespace Rio.Manager.Application.Controls
{
    public class CustomComboBoxColumnItemNameBarcodeDocSep : GridViewBoundColumnBase
    {
        public ObservableCollection<DisplayBarCodeItem> ItemsSource
        {
            get { return (ObservableCollection<DisplayBarCodeItem>)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(ObservableCollection<DisplayBarCodeItem>), typeof(CustomComboBoxColumnItemNameBarcodeDocSep), null);

        public DisplayBarCodeItem SelectedItem
        {
            get { return (DisplayBarCodeItem)GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }
        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register("SelectedItem", typeof(DisplayBarCodeItem), typeof(CustomComboBoxColumnItemNameBarcodeDocSep), null);

        private BarcodeDocumentSeparator barcode;

        public override FrameworkElement CreateCellEditElement(Telerik.Windows.Controls.GridView.GridViewCell cell, object dataItem)
        {
            barcode = dataItem as BarcodeDocumentSeparator;

            if (ItemsSource != null)
            {
                SelectedItem = ItemsSource.Where(i => barcode != null && String.Equals(i.Name, barcode.ItemName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
            }

            return new RadComboBox
            {
                ItemsSource = ItemsSource,
                SelectedItem = SelectedItem,
                DisplayMemberPath = "Name"
            };
        }

        public override FrameworkElement CreateCellElement(Telerik.Windows.Controls.GridView.GridViewCell cell, object dataItem)
        {
            barcode = dataItem as BarcodeDocumentSeparator;

            string name = null;

            if (ItemsSource == null)
            {
                if (barcode != null) name = barcode.ItemName;
            }
            else
            {
                SelectedItem =
                ItemsSource.Where(
                    i => barcode != null && String.Equals(i.Name, barcode.ItemName, StringComparison.InvariantCultureIgnoreCase)).
                    FirstOrDefault();

                if (SelectedItem != null) name = SelectedItem.Name;
            }

            return new TextBlock { Text = name };
        }

        public override object GetNewValueFromEditor(object editor)
        {
            RadComboBox radComboBox = editor as RadComboBox;

            if (radComboBox != null)
            {
                SelectedItem = radComboBox.SelectedItem as DisplayBarCodeItem;
                if (SelectedItem != null) barcode.ItemName = SelectedItem.Name;
            }

            return SelectedItem;
        }
    }
}
